package org.example.tliaswebmanagerment.controller;


import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.example.tliaswebmanagerment.pojo.Dept;


import org.example.tliaswebmanagerment.pojo.Result;
import org.example.tliaswebmanagerment.service.DeptService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//@Scope("prototype") // 多例类型的Bean，每次调用都会创建一个新的对象
@Slf4j
@RestController
public class DeptController {

    @Autowired
    private DeptService deptService;

    //日志对象
    //private static final Logger log = LoggerFactory.getLogger(DeptController.class);


    //查询所有部门
    @GetMapping(value = "/depts")
    public Result list() {
        log.info("查询全部部门");
        List<Dept>deptList=deptService.findAll();
        return Result.success(deptList);

    }

    //删除部门
    //@DeleteMapping(value = "/depts/{id}")
    //public Result delete(@PathVariable Integer id) {
    @DeleteMapping(value = "/depts")
    public Result delete(@RequestParam Integer id) {  //前端传递的请求参数名与方法形参名相同时，该注解可省略
        log.info("删除部门：{}",id);
        deptService.deleteById(id);
        return Result.success();
    }

    //保存部门
    @PostMapping(value = "/depts")
    public Result add(@RequestBody Dept dept) {  //通过实体对象接收json格式数据：{name：××} ，封装到Dept对象中
        // 必须保证Dept中包含name属性
        log.info("保存部门");
        deptService.add(dept);
        return Result.success();
    }

    //根据id查询部门
    @GetMapping(value = "/depts/{id}")
    public Result get(@PathVariable Integer id) {  //@PathVariable注解。直接从URL中获取指定参数值
        log.info("查询部门:{}",id);
        Dept dept=deptService.findById(id);
        return Result.success(dept); //返回部门对象，Result.success(data)方法会返回一个包含数据data的JSON对象
    }


    //修改部门
    @PutMapping(value = "/depts")
    public Result update(@RequestBody Dept dept) {
        log.info("更新部门");
        deptService.update(dept);
        return Result.success();
    }




}
